/*
 * Copyright (C) 2010 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.ui_v4;

import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.Session;
import com.mol.market.common.widget.LoadingDrawable;


/**
 * 我的应用下载管理页
 * 
 * @author andrew
 * @date 2011-11-4
 * @since Version 0.8.3
 */
public class DownloadManagerFragment extends SherlockFragment implements OnItemClickListener {

    private final static int CONTEXT_MENU_DELETE_FILE = 0;
    private final static int CONTEXT_MENU_CANCEL = 1;
    
    /* package */ListView mList;
    private FrameLayout mLoading;
    private TextView mNoData;
    private ProgressBar mProgress;

    private DownloadManagerAdapter mAdapter;
    private DownloadManagerAdapter.AppItem mLongClickItem;

    private Session mSession;
    private SherlockFragmentActivity mActivity;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mActivity = getSherlockActivity();
        mSession = Session.get(mActivity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.activity_apps_manager_layout, container, false);
        doInitView(root);
        return root;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mAdapter.close();
        mAdapter = null;
    }

    public boolean doInitView(View root) {

        mLoading = (FrameLayout) root.findViewById(R.id.loading);
        mProgress = (ProgressBar) mLoading.findViewById(R.id.progressbar);
        mProgress.setIndeterminateDrawable(new LoadingDrawable(mActivity));
        mProgress.setVisibility(View.VISIBLE);
        mNoData = (TextView) mLoading.findViewById(R.id.no_data);
        
        mList = (ListView) root.findViewById(android.R.id.list);
        mList.setEmptyView(mLoading);
        mList.setOnItemClickListener(this);
        mList.setItemsCanFocus(true);
        mAdapter = doInitListAdapter();
        mAdapter.setActivity(this);
        mList.setAdapter(mAdapter);
        mList.setItemsCanFocus(false);
        registerForContextMenu(mList);

        return true;
    }

    public DownloadManagerAdapter doInitListAdapter() {
        return new DownloadManagerAdapter(mActivity, null);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        
        DownloadManagerAdapter.AppItem item = (DownloadManagerAdapter.AppItem) mAdapter.getItem(arg2);
        Intent detailIntent = new Intent(mActivity, ProductDetailActivity.class);
        detailIntent.putExtra(Constants.EXTRA_PACKAGE_NAME, item.mPackageName);
        detailIntent.putExtra(Constants.EXTRA_PRODUCT_NAME, item.mAppName);
        startActivity(detailIntent);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
        mLongClickItem = (DownloadManagerAdapter.AppItem) mAdapter.getItem(acmi.position);
        if (mLongClickItem.mWeight == DownloadManagerAdapter.WEIGHT_WAITING_ITEM
                || mLongClickItem.mWeight == DownloadManagerAdapter.WEIGHT_DOWNLOADING_ERROR_ITEM
                || mLongClickItem.mViewType == DownloadManagerAdapter.VIEW_TYPE_DOWNLOADED) {
            menu.add(1, CONTEXT_MENU_DELETE_FILE, 0, R.string.delete);
        }
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {

        if (item.getGroupId() != 1) {
            return false;
        }

        switch (item.getItemId()) {
        case CONTEXT_MENU_DELETE_FILE:
            mAdapter.delApp(mLongClickItem);
            break;
            
        case CONTEXT_MENU_CANCEL:
            mAdapter.cancelDownloadItem(mLongClickItem);
            break;
            
        default:
            return super.onContextItemSelected(item);
        }
        return true;
    }
    
//    @Override
//    protected void onPrepareDialog(int id, Dialog dialog) {
//        super.onPrepareDialog(id, dialog);
//
//        if (dialog.isShowing()) {
//            dialog.dismiss();
//        }
//    }
    
    /*
     * 刷新无文件状态
     */
    /*package*/ void refreshNoFiles(boolean flag) {
        
        if (flag) {
            mNoData.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.no_apk, 0, 0);
            mNoData.setText(R.string.no_apk_file);
            mNoData.setCompoundDrawablePadding(10);
            mNoData.setVisibility(View.VISIBLE);
            mProgress.setVisibility(View.GONE);
        } else {
            mNoData.setVisibility(View.GONE);
            mProgress.setVisibility(View.VISIBLE);
        }
        
    }
}
